package org.net.cms.mapper;

import org.apache.ibatis.annotations.*;
import org.net.cms.controller.app.payment.vo.AppPaymentReqVO;
import org.net.cms.dao.PaymentDto;

import java.util.List;

@Mapper
public interface PaymentMapper {

    /**
     * 根据主键查询单个缴费记录
     */
    @Select("SELECT id, parentId, studentId, name, payType, amount, status, deleted " +
            "FROM payment WHERE id = #{id} AND deleted = 0")
    PaymentDto selectById(Integer id);

    /**
     * 查询所有未删除的缴费记录（查列表）
     */
    @Select("SELECT * FROM payment WHERE deleted = 0 and parentId = #{parentId} ORDER BY id DESC")
    List<PaymentDto> selectAll(int parentId);

    /**
     * 根据缴费状态查询所有未删除的缴费记录（查列表）
     */
    @Select("SELECT * FROM payment WHERE deleted = 0 and status = #{status} ORDER BY id DESC")
    List<PaymentDto> selectAllByStatus(int status);

    /**
     * 新增缴费记录
     */
    @Insert("INSERT INTO payment (parentId, studentId, name, payType, amount, status) " +
            "VALUES (#{parentId}, #{studentId}, #{name}, #{payType}, #{amount}, #{status})")
    int insert(PaymentDto payment);

    /**
     * 更新缴费记录
     */
    @Update("UPDATE payment " +
            "SET amount = #{amount}, " +
            "    status = #{status} " +
            "WHERE id = #{id} AND deleted = 0")
    int updateById(AppPaymentReqVO payment);

    /**
     * 逻辑删除缴费记录
     */
    @Update("UPDATE payment SET deleted = 1 WHERE id = #{id} AND deleted = 0")
    int deleteById(Integer id);

    // 根据学生id删除缴费信息
    @Update("update payment set deleted = 1 where studentId = #{studentId}")
    int deletePaymentByStudentId(int studentId);
}
